// pages/showCases/showCases.js
var data_cases = require("../../data/data_cases.js")
Page({
  data: {
    case_list: [], //案例数组
    timeSelection: ['最晚上报', '最早上报'],
    timeSelected: true, //是否点击了按上报时间排序
    byTime: 0, //0代表按最晚上报排序 1代表按最早上报排序
    maskFlag: true, //遮罩层flag
    degreeSelection: ['确诊', '治愈', '死亡'],
    degreeSelected: true, //是否点击的按紧急程度排序的按钮
    byDegree: 0, //0-4分表表示不同程度
  },

  // 跳转修改画面
  toModify(e) {
    var index = e.currentTarget.dataset.index
    var case_data = JSON.stringify(this.data.case_list[index])
    wx.navigateTo({
      url: '../modifyCases/modifyCases?case_data=' + case_data,
    })
  },


  // 显示按上报时间排序的顶部栏
  forTime(e) {
    var timeSelected = !this.data.timeSelected
    this.setData({
      timeSelected: timeSelected
    })
    this.showMask()
  },
  // 显示按状态排序的顶部栏
  forDegree(e) {
    var degreeSelected = !this.data.degreeSelected
    this.setData({
      degreeSelected: degreeSelected
    })
    this.showMask()
  },
  // 状态的选择项
  selectDegree(e) {
    var that = this
    // console.log(e)
    var selected = e.currentTarget.id
    var condition = that.data.degreeSelection[selected]
    that.setData({
      byDegree: selected
    })
    // console.log(condition)
    var list = that.data.case_list.sort(that.compareDegree('condition', condition));
    that.setData({
      case_list: list
    })
    that.forDegree()
  },
  // 比较状态
  compareDegree: function(property, condition) {
    return function(a, b) {
      if (a[property] == condition) {
        value1 = 1
      } else {
        value1 = 0
      }
      if (b[property] == condition) {
        value2 = 1
      } else {
        value2 = 0
      }
      // console.log(value1,value2)
      return value2 - value1;

    }
  },
  // 上报时间的选择项
  selectTime(e) {
    // console.log(e)
    var that = this
    var selected = e.currentTarget.id
    that.setData({
      byTime: selected
    })
    var list = that.data.case_list.sort(that.compareTime('reportDate', selected));
    that.setData({
      case_list: list
    })
    that.forTime()
    // console.log(list)
  },
  // 比较日期
  compareTime: function(property, way) {
    return function(a, b) {
      var value1 = Date.parse(a[property]);
      var value2 = Date.parse(b[property]);
      if (way == 0) {
        return value2 - value1;
      } else {
        return value1 - value2;
      }
    }
  },
  // 展示遮罩层
  showMask() {
    var maskFlag = !this.data.maskFlag
    this.setData({
      maskFlag: maskFlag
    })
  },
  // onLoad函数
  onLoad(e) {
    // 这里向后台请求数据
    var case_list = data_cases.data.cases
    this.setData({
      case_list: case_list
    })
    console.log(case_list)
  },
  // onShow函数
  onShow(e){
    // 这里向后台请求数据
  
  }
})